package com.cmg.services.qrcode.controller;

import com.cmg.services.qrcode.service.QrCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api/services/qrcode")
public class QrCodeController {

    private final QrCodeService qrCodeService;

    public QrCodeController(QrCodeService qrCodeService) {
        this.qrCodeService = qrCodeService;
    }

    /**
     * Test Url: http://localhost:8081/api/services/qrcode/generate?content=123&width=500&height=500
     */
    @GetMapping("/generate")
    public void generate(@RequestParam(name = "content") String content,
                         @RequestParam(name = "width", defaultValue = "300") int width,
                         @RequestParam(name = "height", defaultValue = "300") int height,
                         HttpServletResponse response) throws Exception {
        log.info("生成二维码图片，内容：{}，长：{}，宽：{}", content, width, height);
        qrCodeService.createQrCode2Stream(content, width, height, response.getOutputStream());
    }
}
